package com.gym.myshop.dao.impl;

import com.gym.myshop.dao.ProductDao;
import com.gym.myshop.entity.Product;
import com.gym.myshop.entity.Type;
import com.gym.myshop.util.DBUtil;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.MapHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;

import java.lang.reflect.InvocationTargetException;
import java.sql.SQLException;
import java.util.List;
import java.util.Map;

/**
 * @author ming
 * 2021/11/6
 * description:
 */
public class ProductDaoImpl implements ProductDao {
    private QueryRunner qr = new QueryRunner();

    /**
     *@author:ming
     *@Date: 2021/11/6
     *@description:获取数据总行数
     */
    @Override
    public Long getCount(int tid) {
        try {
            return qr.query(DBUtil.getConnection(), "select count(pid) from product where tid=?", new ScalarHandler<>()
                    , tid);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * @author:ming
     * @Date: 2021/11/6
     * @description:分页显示
     */
    @Override
    public List<Product> getProByPage(int tid, int startRow, int pageSize) {
        try {
            return qr.query(DBUtil.getConnection()
                    , "select * from product where tid= ? limit ?,? ", new BeanListHandler<>(Product.class),
                    tid, startRow, pageSize);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }


    /**
     *@author:ming
     *@Date: 2021/11/6
     *@description:查询单个种类详情
     */
    @Override
    public Product getProById(Integer pid) {
        try {
            return qr.query(DBUtil.getConnection(), "select * from product where pid = ?",
                    new BeanHandler<>(Product.class), pid);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }


    /**
     *@author:ming
     *@Date: 2021/11/6
     *@description:联表查数据，商品分类
     */
    @Override
    public Product getProByIdWithType(Integer pid) {
        try {
            // 1 使用MapHandler 查询单个数据,  MapListHandler 查询多行数据
            // MapHandler 查询数据  key字段名 value 字段名对应的数据===>(key)pid=(value)9
            Map<String, Object> query = qr.query(DBUtil.getConnection(),
                    "select * from product p,type t  where t.tid=p.tid and  pid = ? ",
                    new MapHandler(), pid);
            //java类工具包=>进行数据封装的方法
            if (query != null) {
                //2 创建 数据封装数据的类对象
                Product pro = new Product();
                Type type = new Type();
                //3 调用工具包方法,传入需要封装的对象以及保存数据的map集合
                //他会根据map的key值找到对应属性自己进行调用set方法设值
                BeanUtils.populate(pro, query);
                BeanUtils.populate(type, query);
                //4 把封装好的对象数据设置
                pro.setType(type);
                return pro;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
        return null;
    }
}
